var BackofficeMAdList  = {
	 
	
	load : function () {
		var qs =  new QueryString();
		var customAccount =qs.get("customAccount"); //"koko0809@msn.com";//qs.get("customAccount");
		this.loadCustomInfo(customAccount);
		this.createGrid(customAccount);
		 
	    Ext.getDom("welcome").innerHTML =  getUser().Name;	
	    Ext.get("btnAdd").on('click',this.toAdd);  //確認,新增使用者	
	},
	
	
	toAdd:function () {
		var qs =  new QueryString();
		var customAccount =qs.get("customAccount");  
		location.href='/ad/sys/com/control/AdService.php?action=toAdd&account='+customAccount
	},
	
	loadCustomInfo : function (account) {
		Ext.Ajax.request({
			url : "/ad/sys/com/control/UserService.php?action=getUserNotStatus&account="+account,			
			callback  : function (op, su, rsp) {
               var custom =  rsp.responseText.toJSON();
               Ext.getDom("name").value = custom.Name;
               Ext.getDom("phone").value = custom.Phone;
               Ext.getDom("count").value = custom.Count + "點";
               
              
               var typeText = "";
               //身分 0 管理員,1媒體代理商 2一般 3 開發者
               switch(custom.Type) {
               	  case "0" : typeText = "管理員"; break;
               	  case "1" : typeText = "媒體代理商"; break;
               	  case "2" : typeText = "一般會員"; break;
               	  case "3" : typeText = "開發者"; break;
               }
               Ext.getDom("type").value = typeText; 
               
			}
		}); 	
	},
	
	openAdDetail : function (adId) {
	 
		var url = "/ad/html/backoffice/AdDetail.php?adId=" + adId
		var width =800;
		var  height = 600;	
		window.showModalDialog(url,null,'dialogHeight:'+height+'pt;dialogWidth:'+800+'pt;center:yes;status=no');
		
		BackofficeMAdList.store.reload();
		
	},
	toEdit : function (adId) {		
		location.href="/ad/sys/com/control/AdService.php?action=managerStep1Action&adId=" + adId;
	
	},
	
	createGrid : function (account) {
		 BackofficeMAdList.store = new Ext.data.JsonStore ({
	 	 	url:"/ad/sys/com/control/AdService.php?action=getAdReport&account="+account,
			root : "row",
			method : "GET",
			remoteSort: false,
			totalProperty: 'count',
			fields : ["Id","AdName", "AdStartDate", "AdEndDate", "Budget","Status"]
		});
	 
		BackofficeMAdList.grid =new Ext.grid.GridPanel({
			renderTo : "grid",
			store :  BackofficeMAdList.store,
			height: 340,
			loadMask : true,
			viewConfig : {
				forceFit : true 
			},
			columns : [ 
				new Ext.grid.RowNumberer(),
				{header : "名稱" ,dataIndex : "AdName", width :60,sortable : true},
				{header : "開始時間", dataIndex : "AdStartDate",width :30, sortable : true},
				{header : "結束時間", dataIndex : "AdEndDate",width :30,sortable : true},
				{header : "總預算", dataIndex : "Budget",width :30 , sortable : true}, 
				{header : "狀態", dataIndex : "Status" , width : 30, sortable : true,
					renderer : function (value, p , record) {
						
						//廣告狀態 0.審理中 1.刊登中 2.已結束,3 暫停 4.刪除
						var statusText = "";
						switch(value) {
							case "0" : statusText = "審理中"; break;
							case "1" : statusText = "刊登中"; break;
							case "2" : statusText = "已結束"; break;
							case "3" : statusText = "暫停"; break;
							case "4" : statusText = "刪除"; break;
							case "5" : statusText = "未通過"; break;
							case "6" : statusText = "需修改"; break;
						}
						
						if (value == "0") {
							return String.format("<a href=javascript:BackofficeMAdList.openAdDetail({0})>{1}</a>"  ,record.data.Id , statusText);
						} else {
							return statusText;
						}
						
					}
				},
				{header : "", width : 40, renderer: function (v,p,r) {
					return String.format('<input type="button"  class="gridBtn" value="編輯" onclick="BackofficeMAdList.toEdit({0})" />', r.data.Id);   	
				}}
			],
			
			bbar : new Ext.PagingToolbar({
				pageSize :10,
				store : BackofficeMAdList.store,
				displayInfo : true,
				displayMsg : "顯示第 {0} 筆到 {1} 筆紀錄,共 {2} 筆紀錄",
				emptyMsg : "沒有紀錄" 
			}) 
		});	
		
		BackofficeMAdList.store.load({
			params:{ start:0, limit:10} 
		});    
	}
}


Ext.onReady(function (){
	BackofficeMAdList.load();
});